<?php
class LoginController extends Zend_Controller_Action {
	
	/**
	 * 
	 * @access public
	 * @return 
	 */
	public function init(){
//		session_start();
		$this->chkLogin();
	}
	/**
	 * 
	 * @access public
	 * @return 
	 */
	public function indexAction(){
		$this->chkLogin();
	}
	/**
	 * 检测是否登录,未登录则显示登录页面
	 * @access public
	 * @return void
	 */
	private function chkLogin(){
		if (isset($_SESSION['user']) && ($_SESSION['user']['login']=="Y")) {
			$this->_redirect("/admin/");
		}else {
			return ;
		}
	}
	/**
	 * 登录验证
	 * @access public
	 */
	public function authAction(){
		if (isset($_REQUEST['username'] , $_REQUEST['password'])) {
			if(empty($_REQUEST['username']) || empty($_REQUEST['password'])){
				die("用户名或密码不能为空！");
			}
			$username = $_REQUEST['username'];
			$password = $_REQUEST['password'];
			
			$auth = User::auth($username,$password);
			if ($auth) {
				session_start();
				$user = new User($username);
				$this->user = &$user;

				$_SESSION['user']['login'] = "Y";
				
				$act = $this->user->getUserAct();
				$_SESSION['user']['useract'] = &$act;
				
				$column = $this->user->getColumn();
				$_SESSION['user']['column'] = $column;
				
				$_SESSION['user']['username'] = $username;
				$_SESSION['user']['userinfo'] = $user->getUserInfo();

				$this->view->user = $user;
			} else {
				$this->view->msgSection = "密码错误";
			}

			$this->_redirect("/admin/index");
		} else {

			$this->_redirect('/admin/login');
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	
}